package yxy.game.pm2.command.passbarrier;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.passbarrier.Monster;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.ArrayList;


/****************************************
 * @name CMD_PassBarrierDegreeChange
 * @description 斩关夺隘主难度修改
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierDegreeChange, classtype = PassBarrierMessage.PassBarrierDegreeChangeRequest.class)
public class CMD_PassBarrierDegreeChange extends Command<PassBarrierMessage.PassBarrierDegreeChangeRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierDegreeChangeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int type = request.getType();  // 1 为加难度  0 为降难度
        PassBarrierMessage.PassBarrierDegreeChangeResponse.Builder response = PassBarrierMessage.PassBarrierDegreeChangeResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            ArrayList<Monster> monsterList = module.getMonsterList();

            int degree = module.getDegree();
            if (degree <= 3 && type == 0) {
                send(messageSource, response, Result.PassBarrierDegreeCantReduce);//难度不能过低
                return;
            }

            if (degree >= module.getMaxDegree() && type == 1) {
                send(messageSource, response, Result.PassBarrierDegreeCantIncrease);//难度不能过高
                return;
            }

            //todo 改变难度
            for (Monster monster : monsterList) {
                if (type == 0) {
                    int currentDegree = monster.getDegree() - 1 <= 0 ? 1 : monster.getDegree() - 1;
                    monster.setDegree(currentDegree);
                }
                if (type == 1) {
                    monster.setDegree(monster.getDegree() + 1);
                }
            }

            //以第一个难度为准
            module.setDegree(monsterList.get(0).getDegree());
            module.set();

            response.setCurrentDegree(monsterList.get(0).getDegree());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierDegreeChangeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
